package com.utils {
	import flash.display.MovieClip;
	import flash.text.engine.BreakOpportunity;
	import flash.text.engine.EastAsianJustifier;
	import flash.text.engine.ElementFormat;
	import flash.text.engine.JustificationStyle;
	import flash.text.engine.LineJustification;
	import flash.text.engine.TextBaseline;
	import flash.text.engine.TextBlock;
	import flash.text.engine.TextElement;
	import flash.text.engine.TextLine;
	import flash.text.engine.TextRotation;

	public class TextSplider {
		public function TextSplider() {
		}

		public static function splide(container:MovieClip, text:String, fontSize:int = 14, fontColor:uint = 0xffffff):void {
			var tb:TextBlock = new TextBlock();
			//			var shijingText:String = "滚滚长江东逝水，浪花淘尽英雄。是非成败转头空。青山依旧在，几度夕阳红。 白发渔樵江渚上，惯看秋月春风。一壶浊酒喜相逢。古今多少事，都付笑谈中。";
			var format:ElementFormat = new ElementFormat();
			var linePosition:int = container.width - 10;
			clearTextSplider(container);
			format.fontSize = fontSize;
			format.color = fontColor;
			format.breakOpportunity = BreakOpportunity.ANY;
			format.textRotation = TextRotation.AUTO;
			format.alignmentBaseline = TextBaseline.IDEOGRAPHIC_CENTER;
			tb.lineRotation = TextRotation.ROTATE_90;
			tb.textJustifier = new EastAsianJustifier("zh_CN", LineJustification.ALL_BUT_LAST, JustificationStyle.PUSH_IN_KINSOKU);
			tb.content = new TextElement(text, format);
			var previousLine:TextLine = null;
			while (true) {
				var textLine:TextLine = tb.createTextLine(previousLine, 190);
				if (textLine == null)
					break;
				textLine.y = 0;
				textLine.x = linePosition - textLine.width;
				textLine.filters = [Filter.filter2];//[new GlowFilter(0x000000, 1, 2, 2, 10, 1, false, false)];
				linePosition -= 25;
				container.addChild(textLine);
				previousLine = textLine;
			}
		}

		public static function clearTextSplider(container:MovieClip):void {
			var needRemove:Array = [];
			for (var i:int = 0; i < container.numChildren; i++) {
				if (container.getChildAt(i) is TextLine) {
					needRemove.push(container.getChildAt(i));
				}
			}
			while (needRemove.length > 0) {
				container.removeChild(needRemove.shift());
			}
		}
	}
}
